
package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.JedisUtil;
import com.platform.common.utils.Query;
import com.platform.modules.mall.dao.MallWinPrizeDao;
import com.platform.modules.mall.entity.MallPrizeEntity;
import com.platform.modules.mall.entity.MallUserEntity;
import com.platform.modules.mall.entity.MallWinPrizeEntity;
import com.platform.modules.mall.service.MallWinPrizeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 中奖记录表Service实现类
 *
 * @author 
 * @date 2021-05-26 08:39:02
 */
@Service("mallWinPrizeService")
public class MallWinPrizeServiceImpl extends ServiceImpl<MallWinPrizeDao, MallWinPrizeEntity> implements MallWinPrizeService {

    @Resource
    JedisUtil jedisUtil;

    @Override
    public List<MallWinPrizeEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.ADD_TIME");
        params.put("asc", false);
        Page<MallWinPrizeEntity> page = new Query<MallWinPrizeEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallWinPrizePage(page, params));
    }

    @Override
    public boolean add(MallWinPrizeEntity mallWinPrize) {
        mallWinPrize.setAddTime(new Date());
        return this.save(mallWinPrize);
    }

    @Override
    public boolean update(MallWinPrizeEntity mallWinPrize) {
        return this.updateById(mallWinPrize);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    //写入中奖记录
    public void setWinPrize(MallUserEntity userEntity, MallPrizeEntity win,boolean tag) {
        MallWinPrizeEntity winPrizeEntity = new MallWinPrizeEntity();
        winPrizeEntity.setActivityId(win.getActivityId());
        winPrizeEntity.setCommodityName(win.getPrizeName());
        winPrizeEntity.setCommodityId(win.getCommodityId());
        winPrizeEntity.setType(win.getType());
        winPrizeEntity.setUserId(userEntity.getId());
        winPrizeEntity.setCommodityImg(win.getImage());
        winPrizeEntity.setUserMobile(userEntity.getMobile());
        winPrizeEntity.setUserName(userEntity.getNickname() + "(" + userEntity.getUserName() + ")");
        this.add(winPrizeEntity);
        // 中奖后加入缓存信息
        if(tag){
            String mobile = winPrizeEntity.getUserMobile();
            mobile = mobile.substring(0, 3) + "****" + mobile.substring(7);
            String date = DateUtils.format(winPrizeEntity.getAddTime(), "MM-dd HH:mm");
            //写入redis中奖队列
            jedisUtil.lpush("WinPrizeList", String.format("{num:\"%s\",goods:\"%s\"}", mobile, win.getPrizeName(),date));
            //只缓存20条最新中奖消息
            jedisUtil.ltrim("WinPrizeList", 0, 19);
        }
    }
}
